// pages/home/home.js
const APP=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data:{
    pictures:["1.jpg","2.jpg","3.jpg"],
    // text: "陕西省西安市西安电子科技大学南校区竹园一号书院计科院活动室",
    text:"",
    marqueePace: 0.5,//滚动速度
    marqueeDistance: 0,//初始滚动距离
    marqueeMargin: 30,
    size:35,
    marqueeWidth:660,
  },
  tapEvent: function(e) {
    console.log(e)
    let url=e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    wx.cloud.callFunction({
      name:'notice',
      data:{
        type:"checkNotice"
      },
      success(res){
        console.log(res)
        if (res.result.msg=='success'){
          that.setData({
            text:res.result.result
          })
          that.Cal_msg()
        }else{
        wx.showToast({
          title: res.result.msg,
          icon:'none'
        })     
      }
        
      },
      fail(res){
        console.log(res)
        wx.showToast({
          icon:"error",
          title: '云函数调用失败',
        })
      }
    })
    let G_user=APP.globalData.user
    this.setData({
      user:G_user
    })
    // console.log('onload',this.data.user)
  },
  Cal_msg:function(){
    console.log(this.data.text)
    var that = this;
    var length = that.data.text.length * that.data.size;
    that.setData({
      length : length,
      marqueeMargin: length <that.data.marqueeWidth?(that.data.marqueeWidth-length)/4:that.data.marqueeMargin
    })
    if(that.data.length>that.data.marqueeWidth){
      that.run1();
    }
  },
  ShowMsg:function(){
    let that=this;
    wx.showModal({
      content:that.data.text
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  run1: function () {
    var that = this;
    var mytime = setInterval(function () {
      if (-that.data.marqueeDistance < that.data.length) {
        that.setData({
          marqueeDistance: that.data.marqueeDistance - that.data.marqueePace,
        })
      } else {
        clearInterval(mytime);
        that.setData({
          marqueeDistance: that.data.marqueeWidth
        });
        that.run1();
      }
    }, 5)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})